/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import lineage.ui.customize.L1CustomizeScrollPane;
import lineage.ui.customize.L1Font;

/**
 * 多行文本框.
 * 
 * @version 2014年2月12日下午1:18:26
 * @author jrwz
 */
public class L1TextArea extends JTextArea implements FocusListener, KeyListener {
    private static final long serialVersionUID = 3621521335278264022L;
    /** 字体大小(默认13). */
    protected float fontSize = 13.0f;
    /** 组件名称. */
    protected String name;
    /** 组件的x坐标. */
    protected int _x;
    /** 组件的y坐标. */
    protected int _y;
    /** 组件的宽度. */
    protected int _width;
    /** 组件的高度. */
    protected int _height;
    /** 滚动条的x坐标. */
    protected int scrollPaneX;
    /** 滚动条的y坐标. */
    protected int scrollPaneY;
    /** 滚动条的宽度. */
    protected int scrollPaneWidth;
    /** 滚动条的高度. */
    protected int scrollPaneHeight;
    /** 预设要显示的内容. */
    protected String presetText;
    /** 滚动条. */
    protected JScrollPane jsp;

    protected L1TextArea() {
    }

    /**
     * 初始化.
     * 如要添加其他组件,应在此方法之前.
     * 
     * @param flag
     *            - 是否立即显示
     */
    protected void init(boolean flag) {
        setLayout();
        setFont();
        setName(name); // 设置组件的名称(标识符)
        setBounds(_x, _y, _width, _height);
        addListener(); // 增加监听器
        scrollPaneX = _x;
        scrollPaneY = _y;
        scrollPaneWidth = _width;
        scrollPaneHeight = _height;
        setText(presetText);
        setVisible(flag);
    }

    /**
     * 设置布局(默认空布局).
     */
    protected void setLayout() {
        setLayout(null); // 设置空布局(其上的组件可以任意调整位置)
    }

    /**
     * 设置字体(默认微软雅黑13号).
     */
    protected void setFont() {
        L1Font.setFont(Font.PLAIN, fontSize);
        setFont(L1Font.getFont()); // 设置字体
    }

    /**
     * 增加监听器(默认增加键盘事件监听与焦点状态监听).
     */
    protected void addListener() {
        addKeyListener(this); // 增加键盘事件监听
        addFocusListener(this); // 增加焦点状态监听
    }

    /**
     * 取得预先要显示的字符串.
     * 
     * @return 预先要显示的字符串内容
     */
    public String getPresetText() {
        return presetText;
    }

    /**
     * 设置预先要显示的字符串.
     * 
     * @param s
     *            - 预先要显示的字符串
     */
    public void setPresetText(String s) {
        presetText = s;
    }

    /**
     * 设置显示状态.
     * 
     * @param flag
     *            - true：显示, false：不显示
     */
    public void setShow(boolean flag) {
        setVisible(flag);
        jsp.setVisible(flag);
    }

    private class NL1ScrollPane extends L1CustomizeScrollPane {
        private static final long serialVersionUID = -3137998908119452605L;

        protected NL1ScrollPane(Component view) {
            super(view);
            // 一般不要设置
            _x = scrollPaneX;
            _y = scrollPaneY;
            _width = scrollPaneWidth - 4;
            _height = scrollPaneHeight - 3;
            // end
            init(true);
        }
    }

    /**
     * 取得带滚动条的多行文本框.
     * 
     * @return 滚动条+多行文本框
     */
    public JScrollPane addJSP() {
        jsp = new NL1ScrollPane(this); // 添加滚动条
        return jsp;
    }

    /**
     * 键入某个键时调用.
     */
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO 自动生成的方法存根

    }

    /**
     * 按下某个键时调用.
     */
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO 自动生成的方法存根

    }

    /**
     * 释放某个键时调用.
     */
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO 自动生成的方法存根

    }

    /**
     * 组件获得键盘焦点时调用.
     */
    @Override
    public void focusGained(FocusEvent e) {
        // TODO 自动生成的方法存根

    }

    /**
     * 组件失去键盘焦点时调用.
     */
    @Override
    public void focusLost(FocusEvent e) {
        // TODO 自动生成的方法存根

    }
}
